home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Graphics / GraphicsWrap / Source / GraphicView.m < prev    next >
Text File  |  1991-09-18  |  7KB  |  313 lines

  1. /* GraphicView.m -- The meaty part of the front end.
  2.  *
  3.  * Written By: Bill Bumgarner (Friday Software & Consulting)
  4.  *             <wb1j+@andrew.cmu.edu>
  5.  *             414 S.Craig, #119
  6.  *             Pittsburgh, PA. 15213
  7.  *             412-268-5378
  8.  *
  9.  *  This class drives the bitmap object-- it handles events, does
  10.  *  initialization and provides the interface between the bitmap and
  11.  *  the user interface.
  12.  */
  13.  
  14. #import "GraphicView.h"
  15. #import "TGIF.h"
  16.  
  17. #import "NXBitmapGraphicRep.h"
  18. #import <appkit/Application.h>
  19. #import <appkit/Slider.h>
  20. #import <appkit/Window.h>
  21. #import <appkit/color.h>
  22. #import <appkit/graphics.h>
  23. #import <math.h>
  24. #import <libc.h>
  25.  
  26. #import "CmdVertex.h"
  27. #import "CmdBgnpoly.h"
  28. #import "CmdEndpoly.h"
  29. #import "AbsPoly.h"
  30.  
  31. // Pre-defined Color Depths (NX_EightBitGrayDepth is not supported yet):
  32. //    NX_DefaultDepth
  33. //    NX_TwoBitGrayDepth
  34. //    NX_EightBitGrayDepth
  35. //    NX_TwelveBitRGBDepth
  36. //    NX_TwentyFourBitRGBDepth
  37.  
  38. // NeXT Pre-defined Colors:
  39. //    NX_COLORBLACK
  40. //    NX_COLORWHITE
  41. //    NX_COLORGRAY
  42. //    NX_COLORLTGRAY
  43. //    NX_COLORDKGRAY
  44. //    NX_COLORRED
  45. //    NX_COLORGREEN
  46. //    NX_COLORBLUE
  47. //    NX_COLORCYAN
  48. //    NX_COLORYELLOW
  49. //    NX_COLORMAGENTA
  50. //    NX_COLORORANGE
  51. //    NX_COLORPURPLE
  52. //    NX_COLORBROWN
  53. //    NX_COLORCLEAR
  54.  
  55. @implementation GraphicView
  56. id tgif;
  57. BOOL polyDrawing=NO;
  58. BOOL lineDrawing=YES;
  59. id curPoly;
  60.  
  61. - initFrame:(NXRect *) aRect
  62. {
  63.   [super initFrame:aRect];
  64.   bitmap=[[NXBitmapGraphicRep alloc] initWithSize:&(aRect->size)
  65.       // change this to one of the predefined depths if you want to work
  66.       // in other than two bit gray mode.  As long as you use the
  67.       // plotPoint method (or write your own color-handling bit
  68.                    // manipulation stuff)
  69.       // the NXBitmapGraphicRep class will deal w/the different color
  70.       // models automatically.  Two change the color model for an existing
  71.       // program, simply change the constant below and recompile.
  72.       // currently, changing the color model on the fly is not supported
  73.       // (though it is not that difficult to do!).
  74.       depth:NX_TwoBitGrayDepth
  75.       andColor:NX_COLORDKGRAY];
  76.   [self setFlipped:NO];
  77.   return self;
  78. }
  79.  
  80. - infoPanel:sender
  81. {
  82.   if (infoPanel == nil) {
  83.     infoPanel = [NXApp loadNibSection:"InfoPanel.nib" owner:self withNames:NO];
  84.     [infoPanel center];
  85.   }
  86.   [infoPanel orderFront:sender];
  87.  
  88.   return self;
  89. }
  90.  
  91. // this method is called immediately after all IB objects are created and
  92. // initialized and immediately before applicaton receives a -run message.
  93. -appDidInit:sender
  94. {
  95.   float gray;
  96.  
  97.   [bitmap setColor:NX_COLORLTGRAY];
  98.  
  99.   NXConvertColorToGray([bitmap color], &gray);
  100.   [foreWell  setColor:[bitmap color]];
  101.   [foreWell setContinuous:YES];
  102.   [foreSlide setFloatValue:gray];
  103.  
  104.   [backWell setColor:[bitmap backColor]];
  105.   [backWell setContinuous:YES];
  106.   NXConvertColorToGray([bitmap backColor], &gray);
  107.   [backSlide setFloatValue:gray];
  108.   srandom(getpid());
  109.   // start in line-drawing mode
  110.   polyDrawing=NO;
  111.   lineDrawing=YES;
  112.  
  113.   tgif=[[TGIF alloc] initWithBitmap:bitmap andView:self];
  114.   return self;
  115. }
  116.  
  117. - drawSelf:(const NXRect *)rects :(int)rectCount
  118. {
  119.   [bitmap draw];
  120.   return self;
  121. }
  122.  
  123. long randnum(min, max)
  124.      long min, max;
  125. {
  126.   return (max-min)*(random()/(pow(2,31)-1))+min;
  127. }
  128.  
  129. - draw:sender
  130. {
  131.   int i;
  132.   // normally, the [self display] would be done after ALL drawing.  It is
  133.   // done this way only as a throughput test.
  134.   [bitmap setColor:NX_COLORBLACK];
  135.   for(i=0;i<1000;i++) {
  136.     [bitmap line:randnum(0,639) :randnum(0,479)
  137.      :randnum(0,639) :randnum(0,479)];
  138.   }
  139.   [self display];
  140.   
  141.   return self;
  142. }
  143.  
  144.  
  145. - setFore:sender
  146. {
  147.   NXColor c=NXConvertGrayToColor([sender floatValue]);
  148.  
  149.   if (!NXEqualColor(c, [bitmap color])){
  150.     [bitmap setColor:c];
  151.     [foreWell setColor:c];
  152.     [tgif cmdForeColor:c];
  153.   }
  154.     return self;
  155. }
  156.  
  157. - setBack:sender
  158. {
  159.   NXColor c=NXConvertGrayToColor([sender floatValue]);
  160.   
  161.   if (!NXEqualColor(c, [bitmap backColor])){
  162.     [bitmap setBackColor:c];
  163.     [backWell setColor:c];
  164.     [tgif cmdBackColor:c];
  165.     [bitmap eraseFrame];
  166.     [self display];
  167.   }
  168.   return self;
  169. }
  170.  
  171. - startStopPolyDraw:sender
  172. {
  173.   if(polyDrawing){
  174.     polyDrawing=NO;
  175.     // initialize poly data struct
  176.   } else {
  177.     polyDrawing=YES;
  178.     // draw the polygon
  179.   }
  180.   return self;
  181. }
  182.  
  183. NXPoint start,end;
  184. - mouseDown:(NXEvent *)theEvent
  185. {
  186.   start=theEvent->location;
  187.   [self convertPoint:&start fromView:nil];
  188.   if(!polyDrawing)
  189.     [tgif cmdMove:(int)start.x :(int)start.y];
  190.   return self;
  191. }
  192.  
  193. - mouseUp:(NXEvent *)theEvent
  194. {
  195.   end=theEvent->location;
  196.   [self convertPoint:&end fromView:nil];
  197.  
  198.   if(!polyDrawing){
  199.     [bitmap line:(int)start.x :(int)start.y :(int)end.x :(int)end.y];
  200.     [tgif cmdDraw:(int)end.x :(int)end.y];
  201.     [self display];
  202.   }
  203.   else
  204.     [curPoly addCommand:[[CmdVertex alloc] initCmd:end.x :end.y]];
  205.   return self;
  206. }
  207.  
  208. -eraseImage:sender
  209. {
  210.   // erase the bitmap to the current backColor
  211.   [bitmap eraseFrame];
  212.   [tgif cmdNewFrame];
  213.   // redisplay bitmap
  214.   [self display];
  215.   return self;
  216. }
  217.  
  218. - foreWell:sender
  219. {
  220.   float gray;
  221.   NXColor c=[sender color];
  222.   if(!NXEqualColor(c, [bitmap color])){
  223.     [bitmap setColor:c];
  224.     NXConvertColorToGray(c, &gray);
  225.     [foreSlide setFloatValue:gray];
  226.     [tgif cmdForeColor:c];
  227.   }
  228.   return self;
  229. }
  230.  
  231. - backWell:sender
  232. {
  233.   float gray;
  234.   NXColor c=[sender color];
  235.   if(!NXEqualColor(c, [bitmap backColor])){
  236.     [bitmap setBackColor:c];
  237.     NXConvertColorToGray(c, &gray);
  238.     [backSlide setFloatValue:gray];
  239.     [bitmap eraseFrame];
  240.     [tgif cmdBackColor:c];
  241.     [self display];
  242.   }
  243.   return self;
  244. }
  245.  
  246. - saveCommands:sender
  247. {
  248.   if(polyDrawing){
  249.     [curPoly addCommand:[[CmdEndpoly alloc] initCmd]];
  250.     [curPoly doCmd];
  251.     [self display];
  252.     polyDrawing=NO;
  253.   }    
  254.   [tgif saveToFile];
  255.   return self;
  256. }
  257.  
  258. - openCommands:sender
  259. {
  260.   if(polyDrawing){
  261.     [curPoly addCommand:[[CmdEndpoly alloc] initCmd]];
  262.     polyDrawing=NO;
  263.   } 
  264.   [tgif openFile];
  265.   return self;
  266. }
  267.  
  268. - newCommands:sender
  269. {
  270.   [tgif newFile];
  271.   return self;
  272. }
  273.  
  274. #define BTN_CROSS 0
  275. #define BTN_ARROW 1
  276. #define BTN_CIRCLE 2
  277. #define BTN_LINE 3
  278. #define BTN_POLY 4
  279. - toolHit:sender
  280. {
  281.   int hitOn=[[sender selectedCell] tag];
  282.   if(polyDrawing){
  283.     // close polygon and do command
  284.     [curPoly addCommand:[[CmdEndpoly alloc] initCmd]];
  285.     [curPoly doCmd];
  286.     [self display];
  287.     polyDrawing=NO;
  288.   }
  289.   switch (hitOn){
  290.   case BTN_CROSS:
  291.   case BTN_ARROW:
  292.     polyDrawing=NO;
  293.     lineDrawing=NO;
  294.     [toolStatus setStringValue:"Selection"];
  295.     break;
  296.   case BTN_LINE:
  297.     lineDrawing=YES;
  298.     [toolStatus setStringValue:"Line Drawing"];
  299.     break;
  300.   case BTN_POLY:
  301.     polyDrawing=YES;
  302.     [toolStatus setStringValue:"Poly Drawing"];
  303.     curPoly=[tgif cmdNewPoly];
  304.     [curPoly addCommand:[[CmdBgnpoly alloc] initCmd]];
  305.     break;
  306.   case BTN_CIRCLE:
  307.     fprintf(stderr, "Circle Drawing not yet implemented\n");
  308.     break;
  309.   }
  310.   return self;
  311. }  
  312. @end
  313.